import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'
// 导入一级组件：
import login from '@/views/login/index.vue'
import layout from '@/views/layout/index.vue'
// 导入二级组件：
import MyHome from '@/views/MyHome/index.vue'
import MyQuestion from '@/views/MyQuestion/index.vue'
import MyVideo from '@/views/MyVideo/index.vue'
import MyUser from '@/views/MyUser/index.vue'
import { Toast } from 'vant'
Vue.use(VueRouter)
const routes = [
  { path: '/login', component: login },
  { path: '/', redirect: '/layout' },
  {
    path: '/layout',
    component: layout,
    children: [
      { path: '/MyHome', component: MyHome },
      { path: '/', redirect: '/MyHome' },
      { path: '/MyQuestion', component: MyQuestion },
      { path: '/MyVideo', component: MyVideo },
      { path: '/MyUser', component: MyUser }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由导航守卫：
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      Toast.fail('请先登录！')
      next('/login')
    }
  }
})
export default router
